package com.test.view;

import java.awt.Font;
import java.sql.Connection;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import com.test.Utils.DBUtils;
import com.test.dao.UserDao;
import com.test.model.User;

public class LogOnFrm extends javax.swing.JFrame {
    DBUtils dbUtil = new DBUtils();
    UserDao userDao = new UserDao();

    /** Creates new form LogOnFrm */
    public LogOnFrm() {
        //改变系统默认字体
        Font font = new Font("Dialog", Font.PLAIN, 12);
        java.util.Enumeration keys = UIManager.getDefaults().keys();
        while (keys.hasMoreElements()) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof javax.swing.plaf.FontUIResource) {
                UIManager.put(key, font);
            }
        }
        initComponents();
        // 设置Frame居中显示
        this.setLocationRelativeTo(null);
    }

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        userNameTxt = new javax.swing.JTextField();
        passwordTxt = new javax.swing.JPasswordField();
        jb_logon = new javax.swing.JButton();
        jb_reset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("学生成绩发布系统");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("微软雅黑", 0, 24));
        jLabel1.setText("学生成绩发布系统");
        jLabel2.setText("用户名");
        jLabel3.setText("密码");
        jb_logon.setText("登录");
        jb_logon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_logonActionPerformed(evt);
            }
        });

        jb_reset.setText("取消");
        jb_reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_resetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());

        getContentPane().setLayout(layout);
        layout
                .setHorizontalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addContainerGap(83, Short.MAX_VALUE)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addGroup(
                                                                                layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                        .addComponent(
                                                                                                jLabel3)
                                                                                        .addComponent(
                                                                                                jLabel2)
                                                                                        .addComponent(
                                                                                                jb_logon))
                                                                        .addGap(
                                                                                39,
                                                                                39,
                                                                                39)
                                                                        .addGroup(
                                                                                layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createParallelGroup(
                                                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                                                false)
                                                                                                        .addComponent(
                                                                                                                passwordTxt,
                                                                                                                0,
                                                                                                                0,
                                                                                                                Short.MAX_VALUE)
                                                                                                        .addComponent(
                                                                                                                userNameTxt,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                                                152,
                                                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                                        .addComponent(
                                                                                                jb_reset))
                                                                        .addGap(
                                                                                89,
                                                                                89,
                                                                                89))
                                                        .addGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addComponent(
                                                                                jLabel1,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                233,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addGap(
                                                                                100,
                                                                                100,
                                                                                100)))));
        layout
                .setVerticalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addGap(44, 44, 44)
                                        .addComponent(jLabel1)
                                        .addGap(64, 64, 64)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel2)
                                                        .addComponent(
                                                                userNameTxt,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(41, 41, 41)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(
                                                                passwordTxt,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jLabel3))
                                        .addGap(31, 31, 31)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jb_logon)
                                                        .addComponent(jb_reset))
                                        .addContainerGap(66, Short.MAX_VALUE)));

        pack();

    }// </editor-fold>
    //GEN-END:initComponents

    private void jb_logonActionPerformed(java.awt.event.ActionEvent evt) {
        String userName = userNameTxt.getText();
        String password = new String(passwordTxt.getPassword());

        String typeAdmin = "admin";

        String theUserType = "" ;
        if (null == userName|| "".equals(userName)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空");
            return;
        }
        if (null == password|| "".equals(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空");
            return;
        }
        User user = new User(userName, password);
        Connection con=null;
        try {
            con=dbUtil.getConnection();
            User currentUser = userDao.login(con, user);
            if (currentUser != null) {

                theUserType = currentUser.getUser_type();
                if(null != theUserType && typeAdmin.equals(theUserType.trim())){
                    this.dispose();
                    new MainFrm().setVisible(true);
                } else {
                    this.dispose();
                    new UserMainFrm().setVisible(true);
                }

            } else {
                JOptionPane.showMessageDialog(null, "用户名或密码错误");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "登录失败");
        }finally{
            try {
                dbUtil.closeCon(con);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    private void jb_resetActionPerformed(java.awt.event.ActionEvent evt) {
        this.userNameTxt.setText("");
        this.passwordTxt.setText("");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LogOnFrm().setVisible(true);
            }
        });
    }

    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jb_logon;
    private javax.swing.JButton jb_reset;
    private javax.swing.JPasswordField passwordTxt;
    private javax.swing.JTextField userNameTxt;

}